//
//  File.swift
//
//
//  Created by Tibor Bodecs on 2022. 03. 06..
//

import Fluent
import Molecular
import Vapor

struct WebResponseController {
    func renderSitemapTemplate(_ req: Request) async throws -> Response {
        let metadatas = try await req.system.metadata.list()
        let template = WebSitemapTemplate(.init(items: metadatas))
        return req.templates.renderXml(template)
    }

    func renderRssTemplate(_ req: Request) async throws -> Response {
        let metadatas = try await req.system.metadata.listFeedItems()
        let template = WebRssTemplate(.init(items: metadatas))
        return req.templates.renderXml(template)
    }

    func renderRobotsTemplate(_ req: Request) async throws -> Response {
        let robots = """
        Sitemap: \(req.molecular.publicUrl)\(req.molecular.config.paths.sitemap.safePath())

        User-agent: *
        Disallow: \(req.molecular.config.paths.admin.safePath())
        Disallow: \(req.molecular.config.paths.api.safePath())
        """
        return Response(status: .ok,
                        headers: [
                            "content-type": "text/plain",
                        ],
                        body: .init(string: robots))
    }
}
